/*
 * @(#)MockActionInvocation.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.test;

import java.util.HashMap;
import java.util.Map;

import com.nhncorp.usf.core.config.ResultTypeInfo;
import com.nhncorp.usf.core.config.runtime.Action;
import com.nhncorp.usf.core.config.runtime.PageInfo;
import com.nhncorp.usf.core.config.runtime.ResultPageInfo;
import com.nhncorp.usf.core.config.runtime.ResultPageInfo.DISPATCHER_TYPE;
import com.nhncorp.usf.core.result.ResultGenerator;
import com.nhncorp.usf.core.xwork.DefaultUsfActionInvocation;
import com.nhncorp.usf.core.xwork.UsfActionInvocation;

import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionProxy;
import com.opensymphony.xwork.Result;
import com.opensymphony.xwork.interceptor.PreResultListener;
import com.opensymphony.xwork.util.OgnlValueStack;


/**
 * Mock {@link UsfActionInvocation}.
 *
 * @author Web Platform Development Team
 */
@SuppressWarnings("serial")
public class MockActionInvocation implements UsfActionInvocation {
	private UsfActionInvocation usfActionInvocation;

	private PageInfo pageInfo;

	private Action actionInfo;

	private ResultGenerator resultGenerator;

	private Map<String, Object> dataMap;

	private boolean initiate;

	/**
	 * Instantiates a new mock action invocation.
	 *
	 * @param pageInfo the page info
	 * @param actionInfo the action info
	 * @param dataMap the data map
	 *
	 * @throws Exception the exception
	 */
	public MockActionInvocation(PageInfo pageInfo, Action actionInfo, Map<String, Object> dataMap) throws Exception {
		// create ResultGenerator
		ResultTypeInfo resultTypesInfo = ResultTypeInfo.getInstance();
		resultTypesInfo.addResultTypeInfo(DISPATCHER_TYPE.mock.name(), "com.nhncorp.usf.core.test.MockResult");
		resultGenerator = new ResultGenerator();

		this.pageInfo = pageInfo;
		this.actionInfo = actionInfo;

		// set the datamap
		this.dataMap = dataMap;
	}

	/**
	 * Instantiates a new mock action invocation.
	 *
	 * @param pageInfo the page info
	 * @param actionInfo the action info
	 *
	 * @throws Exception the exception
	 */
	public MockActionInvocation(PageInfo pageInfo, Action actionInfo) throws Exception {
		this(pageInfo, actionInfo, new HashMap<String, Object>());
	}

	/**
	 * @see com.opensymphony.xwork.ActionInvocation#invoke()
	 * @return String
	 * @throws Exception the Exception
	 */
	public String invoke() throws Exception {
		// create DefaultServiceInvocation in here because of interceptor.
		if (!initiate) {
			usfActionInvocation = new DefaultUsfActionInvocation(new MockServiceExecutor(), resultGenerator, pageInfo,
				actionInfo, dataMap);
			initiate = true;
		}

		return usfActionInvocation.invoke();
	}

	// -- usf action invocation --------------------------------------------------------------------------------------------------

	/**
	 * add {@link BeforeResultDispatch}.
	 * 
	 * @param preResultListener PreResultListener
	 */
	public void addPreResultListener(PreResultListener preResultListener) {
		usfActionInvocation.addPreResultListener(preResultListener);
	}

	/**
	 * Return {@link PageInfo}.
	 * 
	 * @return PageInfo
	 */
	public PageInfo getPageInfo() {
		return this.pageInfo;
	}

	/**
	 * Return {@link Action}.
	 * 
	 * @return Action
	 */
	public Action getActionInfo() {
		return this.actionInfo;
	}

	/**
	 * return {@link Map} contained data from client.
	 * 
	 * @return Map<String, Object>
	 */
	public Map<String, Object> getDataMap() {
		return this.dataMap;
	}

	/**
	 * set the failureCode.
	 * It will be called by Interceptor and used by method named doServiceExecute.
	 * 
	 * @param failureCode String
	 */
	public void setFailureCode(String failureCode) {
		usfActionInvocation.setFailureCode(failureCode);
	}

	/**
	 * @see com.opensymphony.xwork.ActionInvocation#isExecuted()
	 * @return boolean
	 */
	public boolean isExecuted() {
		return usfActionInvocation.isExecuted();
	}

	/**
	 * @see com.nhncorp.usf.core.xwork.UsfActionInvocation#getResultPageInfo()
	 * @return ResultPageInfo
	 */
	public ResultPageInfo getResultPageInfo() {
		return usfActionInvocation.getResultPageInfo();
	}

	// -- ActionContext ----------------------------------------------------------------------------------------------------------

	/**
	 * @see com.opensymphony.xwork.ActionInvocation#getInvocationContext()
	 * @return ActionContext
	 */
	public ActionContext getInvocationContext() {
		return usfActionInvocation.getInvocationContext();
	}

	/**
	 * @see com.opensymphony.xwork.ActionInvocation#getAction()
	 * @return Object
	 */
	public Object getAction() {
		throw new UnsupportedOperationException();
	}

	/**
	 * @see com.opensymphony.xwork.ActionInvocation#getProxy()
	 * @return ActionProxy
	 */
	public ActionProxy getProxy() {
		throw new UnsupportedOperationException();
	}

	/**
	 * @see com.opensymphony.xwork.ActionInvocation#getResult()
	 * @return Result
	 * @throws Exception the Exception
	 */
	public Result getResult() throws Exception {
		throw new UnsupportedOperationException();
	}

	/**
	 * @see com.opensymphony.xwork.ActionInvocation#getResultCode()
	 * @return String
	 */
	public String getResultCode() {
		throw new UnsupportedOperationException();
	}

	/**
	 * @see com.opensymphony.xwork.ActionInvocation#getStack()
	 * @return OgnlValueStack
	 */
	public OgnlValueStack getStack() {
		throw new UnsupportedOperationException();
	}

	/**
	 * @see com.opensymphony.xwork.ActionInvocation#invokeActionOnly()
	 * @return String
	 * @throws Exception the Exception
	 */
	public String invokeActionOnly() throws Exception {
		return null;
	}

	/**
	 * @see com.opensymphony.xwork.ActionInvocation#setResultCode(java.lang.String)
	 * @param arg0 String
	 */
	public void setResultCode(String arg0) {
		throw new UnsupportedOperationException();
	}
}